import { AppContext } from "../app.context";
import { AudioFrequencyInfo } from "../mode/audioFrequency";
import { BaseServices } from "./base.services";
import { APIMethod, APIUrl } from "./parameters";

export type AudioFrequencyResp = {
  code: number
  data: AudioFrequencyInfo[]
  message: string
}

/**
 * 音频相关
 */
export class AudioFrequencyService extends BaseServices {
  private static _instance: AudioFrequencyService;
  public static get instance(): AudioFrequencyService {
    if (this._instance == null) {
      this._instance = new AudioFrequencyService();
    }
    return this._instance;
  }
  constructor() { super() }


  /**
   * 获取歌曲
   * @param account 
   * @param password 
   * @returns 
   */
  list() {
    const list = AppContext.instance.resources.audioFrequency;
    if (list.length > 0) {
      return new Promise<any>(reslove => { reslove(list) })
    };
    const url = APIUrl.AudioFrequency + APIMethod.ApprovedList
    const data = {}
    return new Promise<AudioFrequencyResp>((resolve, reject) => {
      this.request<AudioFrequencyResp>(url, data).then(res => {
        console.log(res);
        if (res.data.length > 0) {
          AppContext.instance.resources.setAudioFrequency(res.data);
        } else {
          wx.showToast({
            title: '暂时没有你想听的歌，稍后再来看看哟',
            icon: 'none',
            duration: 2000
          })
        }

        resolve(res)
      }, err => {
        reject(err);
      })
    });
  }
}